import { FormSchema } from '/@/components/Table';
import { formhandSchema, formbatchSchema } from '../flow-data';
import { FlowHandleBtn } from '../../types/model';
import { SceneTypeEnum } from '/@/components/HChooseUserBase/src/types';

export function getRecipientsSchemaObj(
  options = { field: 'recipients', label: '选择人员', placeholder: '请选择审核人' },
): FormSchema {
  const { field, label, placeholder } = options;

  const recipientsSchemaObj: FormSchema = {
    field,
    label,
    component: 'HChooseUserBase',
    componentProps: {
      placeholder,
    },
  };
  return recipientsSchemaObj;
}

//删除对象已有属性
export function deleteObjAttribute(obj, attr: string | string[]): void {
  if (Array.isArray(attr)) {
    attr.forEach((item) => {
      Reflect.has(obj, item) && delete obj[item];
    });
  } else {
    Reflect.has(obj, attr) && delete obj[attr];
  }
}

export function getBtnFormScheama(type) {
  if (!type) {
    return formhandSchema;
  }

  if (FlowHandleBtn.TurnToDo === type) {
    return formbatchSchema;
  } else {
    return formhandSchema;
  }
} // 设置表单权限
function disHandler(authObj: Indexable = {}) {
  const { operationPermissions } = authObj;

  return {
    required: operationPermissions == 2,
    disabled: operationPermissions == 0,
  };
}

function setComponentProps(receiverLimitItem: Indexable = {}) {
  const { ids, addressType, addressSceneTypeList, apiCodeList: apiCode } = receiverLimitItem;

  const sceneType = addressSceneTypeList && addressSceneTypeList.map((item) => SceneTypeEnum[item]);

  const customProps: Indexable = apiCode ? { apiCode } : { addressType, sceneType };
  customProps.ids = ids;

  return customProps;
}

// 拿选项值去"selectionalRestrictionlist" 匹配 choice， 匹配到了 选里边的权限"operationPermissions"（0代表只读" 1代表可选 2代表必选）， 匹配不到去匹配默认的 choice ：0，0也没匹配到对应的框就隐藏掉（只读权限还没做，默认值显示还没做）
export function getLimitObj(choiceVal, receiverLimitItem, fromSchema) {
  const {
    id,
    type,
    name,
    selectionalRestrictionList: selectionalList,
    record,
    selectedList,
    apiCodeList,
  } = receiverLimitItem;

  const ifShow = ({ values, schema }) => {
    const ret = getIfShow(values, selectionalList);
    if (ret) {
      schema.required = ret.operationPermissions === 2;
      schema.disabled = ret.operationPermissions === 0;
    }

    return !!ret;
  };

  // 拿选项值去"selectionalRestrictionlist" 匹配 choice，
  //匹配到了 选里边的权限"operationPermissions"（0代表只读" 1代表可选 2代表必选），
  //匹配不到去匹配默认的 choice ：0，
  //0也没匹配到对应的框就隐藏掉（只读权限还没做，默认值显示还没做）

  function getIfShow(values, list = []): undefined | Indexable {
    const choiceVal = values['choices'] || 0;
    const ret = list.find(({ choice }) => choice == choiceVal);

    return ret;
  }
  const { componentProps } = fromSchema;

  const componentPropsTemp = setComponentProps(receiverLimitItem);

  const formAuthObj = selectionalList.find(({ choice }) => choice == choiceVal);

  const { required, disabled } = disHandler(formAuthObj);

  const outsideComponent = apiCodeList ? 'HChooseComprehensiveBase' : 'HChooseUserBase';

  const tempSchema: FormSchema = {
    field: `recipients${id}_${type}`,
    label: name,
    component: outsideComponent,
    required,
    ifShow,
    dynamicDisabled: disabled,
    defaultValue: selectedList || [],
    componentProps: Object.assign({}, componentProps, componentPropsTemp, { record }),
  };
  const limitObj = Object.assign({}, fromSchema, tempSchema);

  return limitObj;
}
